/**
 * OSAmI: Open Source Ambient Intelligence
 * Servlets running on the gateway, for displaying data from the sensor nodes.
 * Copyright (C) Jonathan Ferreira 2010 (internship in LCIS-INPG, for a demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */
package org.osami.gateway.display.internal;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Unbind;
import org.osami.gateway.sensor.SenseData;
import org.osami.gateway.storage.ISenseDataStorage;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;

@Component
@Instantiate
public class SensorsViewServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private ISenseDataStorage storage;

    @Bind
    public void bindHttpService(HttpService httpService) throws ServletException, NamespaceException {
        httpService.registerServlet(ServletPaths.SENSORS, this, null, null);
        httpService.registerResources("/resource", "/webContent", null);
    }

    @Unbind
    public void unbindHttpService(HttpService httpService) {
        httpService.unregister(ServletPaths.SENSORS);
    }

    @Bind
    public void bindSenseDataStorage(ISenseDataStorage storage) {
        this.storage = storage;
    }

    private void doGetPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();

        writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
        writer.println("<html lang=\"en\">");

        // HTML Head
        // ---------------------------------------------------
        writer.println("<head>");
        writer.println("<title>Sensor data at " + new Date() + "</title>");
        writer.println("<link href=\"/resource/css/designSensor.css\" rel=\"stylesheet\" type=\"text/css\" />");
        writer.println("<meta http-equiv=\"refresh\" content=\"5\">");
        writer.println("</head>");

        // HTML Body
        // ---------------------------------------------------
        writer.println("<body>");

        writer.println("<H1>List of all sensors</H1><hr/>");

        printAlarmTable(writer);

        writer.println("</body>");
        writer.println("</html>");
    }

    @Override
    synchronized protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        doGetPost(req, resp);
    }

    @Override
    synchronized protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        doGetPost(req, resp);
    }

    private void printAlarmTable(PrintWriter writer) {
        writer.println("<br/><br/>");
        writer.println("<table>");
        // Print table headers
        writer.println("<thead><tr>");
        // Print each column name here
        printAlarmTableHeaders(writer);
        writer.println("</tr></thead>");

        // Print table body
        writer.println("<tbody>");
        // Print each alarm in its own row
        synchronized (storage) {
            for (SenseData sd : storage) {
                printSenseDataRow(writer, sd);
            }
        }
        writer.println("</tbody>");
        writer.println("</table>");
    }

    private void printAlarmTableHeaders(PrintWriter writer) {
        writer.println("<th>SenseId</th>");
        writer.println("<th>Type</th>");
        writer.println("<th>Value</th>");
        writer.println("<th>Unit</th>");
        writer.println("<th>Time</th>");
    }

    private void printSenseDataRow(PrintWriter writer, SenseData sd) {
        writer.println("<tr>");

        printTableCell(writer, "<a href=\"" + ServletPaths.SENSOR + "/" + sd.getSenseId() + "\" >" + sd.getSenseId() + "</a>");
        printTableCell(writer, sd.getType());
        printTableCell(writer, sd.getValue().toString());
        printTableCell(writer, sd.getUnit());

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(sd.getTimeStamp());
        printTableCell(writer, cal.getDisplayName(Calendar.HOUR, Calendar.LONG, Locale.getDefault()));

        writer.println("</tr>");
    }

    private void printTableCell(PrintWriter writer, String content) {
        writer.println("<td>" + content + "</td>");
    }

}
